package cn.school.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.jwt.JWT;
import cn.hutool.jwt.JWTUtil;

import cn.school.dto.FindPaperListDto;
import cn.school.dto.PaperAddDto;
import cn.school.dto.UpdateExamPaperDto;
import cn.school.mapper.ExamPaperMapper;
import cn.school.mapper.PaperDetailMapper;
import cn.school.mapper.SubjectsMapper;
import cn.school.mapper.TeacherMapper;
import cn.school.pojo.ExamPaper;
import cn.school.pojo.PaperDetail;
import cn.school.pojo.Subjects;
import cn.school.pojo.Teacher;
import cn.school.service.ExamPaperService;
import cn.school.vo.FindPaperListVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* @author Lenovo
* @description 针对表【exam_paper(试卷表)】的数据库操作Service实现
* @createDate 2025-06-09 19:39:19
*/
@Service
public class ExamPaperServiceImpl extends ServiceImpl<ExamPaperMapper, ExamPaper>
    implements ExamPaperService {
    @Resource
    private ExamPaperMapper examPaperMapper;
    @Resource
    private TeacherMapper teacherMapper;
    @Resource
    private PaperDetailMapper paperDetailMapper;
    @Resource
    private SubjectsMapper subjectsMapper;
    @Override
    public String addPaper(PaperAddDto paperAddDto, HttpServletRequest request) {
        String token = request.getHeader("token");
        JWT jwt = JWTUtil.parseToken(token);
        Object id = jwt.getPayload("id");
        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",id);
        Teacher teacher = teacherMapper.selectOne(queryWrapper);
        if (teacher == null) {
            return "没有权限添加试卷";
        }else {
            ExamPaper examPaper = new ExamPaper();
            examPaper.setId(IdUtil.getSnowflake(1,1).nextId());
            examPaper.setTeacherId(teacher.getId());
            examPaper.setPublishStatus(0);
            examPaper.setQuestionNumber(paperAddDto.getQuestionNumber());
            String subjectId = paperAddDto.getSubjectId();
            examPaper.setSubjectId(Long.valueOf(subjectId));
            examPaper.setExamTotal(paperAddDto.getExamTotal());
            examPaper.setCreateTime(new Date());
            examPaper.setUpdateTime(null);
            examPaper.setStartTime(paperAddDto.getStartTime());
            examPaper.setEndTime(paperAddDto.getEndTime());
            examPaper.setIsDel(0);
            examPaper.setSessionId(paperAddDto.getExamSessionId());
            int insert = examPaperMapper.insert(examPaper);
            if (insert > 0) {
                return "添加成功";
            }
            return "添加失败";
        }
    }

    @Override
    public List<FindPaperListVo> findPaperList(FindPaperListDto findPaperListDto) {
        QueryWrapper<ExamPaper> queryWrapper = new QueryWrapper<>();
        if (findPaperListDto.getTeacherId() != null && !findPaperListDto.getTeacherId().equals("")) {
            queryWrapper.eq("teacher_id",findPaperListDto.getTeacherId());
        }
        if (findPaperListDto.getSubjectId() != null && !findPaperListDto.getSubjectId().equals("")) {
            queryWrapper.eq("subject_id",findPaperListDto.getSubjectId());
        }
        if (findPaperListDto.getStartTime()!= null && !findPaperListDto.getStartTime().equals("")) {
            queryWrapper.ge("start_time",findPaperListDto.getStartTime());
        }
        if (findPaperListDto.getEndTime()!= null && !findPaperListDto.getEndTime().equals("")) {
            queryWrapper.le("end_time",findPaperListDto.getEndTime());
        }
        queryWrapper.eq("is_del",0);
        Page<ExamPaper> page = new Page<>(findPaperListDto.getPageNum(),findPaperListDto.getPageSize());
        Page<ExamPaper> examPaperPage = examPaperMapper.selectPage(page, queryWrapper);
        List<ExamPaper> records = examPaperPage.getRecords();
        System.err.println(records.toString());
        List<FindPaperListVo> examPage = new ArrayList<>();
        for (ExamPaper record : records) {
            FindPaperListVo findPaperListVo = new FindPaperListVo();
            findPaperListVo.setId(String.valueOf(record.getId()));
            findPaperListVo.setTeacherId(String.valueOf(record.getTeacherId()));
            Teacher teacher = teacherMapper.selectById(record.getTeacherId());
            findPaperListVo.setTeacherName(teacher.getTeacherName());
            findPaperListVo.setPublishStatus(record.getPublishStatus());
            findPaperListVo.setQuestionNumber(record.getQuestionNumber());
            findPaperListVo.setSubjectId(String.valueOf(record.getSubjectId()));
            QueryWrapper<Subjects> subjectsQueryWrapper = new QueryWrapper<>();
            subjectsQueryWrapper.eq("id",record.getSubjectId());
            Subjects subjects = subjectsMapper.selectOne(subjectsQueryWrapper);
            findPaperListVo.setSubjectName(subjects.getSubjectName());
            findPaperListVo.setExamTotal(record.getExamTotal());
            findPaperListVo.setCreateTime(record.getCreateTime());
            findPaperListVo.setUpdateTime(record.getUpdateTime());
            findPaperListVo.setStartTime(record.getStartTime());
            findPaperListVo.setEndTime(record.getEndTime());
            findPaperListVo.setIsDel(record.getIsDel());
            examPage.add(findPaperListVo);
        }
        return examPage;
    }

    @Override
    public String updateExamPaper(UpdateExamPaperDto updateExamPaperDto, HttpServletRequest request) {
        String token = request.getHeader("token");
        JWT jwt = JWTUtil.parseToken(token);
        Object id = jwt.getPayload("id");
        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",id);
        Teacher teacher = teacherMapper.selectOne(queryWrapper);
        if (teacher == null) {
            return "没有权限修改试卷信息";
        }else {
            QueryWrapper<ExamPaper> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("id",updateExamPaperDto.getId());
            ExamPaper examPaper = examPaperMapper.selectOne(queryWrapper1);
            examPaper.setQuestionNumber(updateExamPaperDto.getQuestionNumber());
            String subjectId = updateExamPaperDto.getSubjectId();
            examPaper.setSubjectId(Long.valueOf(subjectId));
            examPaper.setExamTotal(updateExamPaperDto.getExamTotal());
            examPaper.setStartTime(updateExamPaperDto.getStartTime());
            examPaper.setUpdateTime(new Date());
            examPaper.setEndTime(updateExamPaperDto.getEndTime());
            examPaper.setSessionId(updateExamPaperDto.getExamSessionId());
            int update = examPaperMapper.updateById(examPaper);
            if (update > 0) {
                return "修改成功";
            }
            return "修改失败";
        }
    }

    @Override
    public String deleteExamPaper(String paperId, HttpServletRequest request) {
        String token = request.getHeader("token");
        JWT jwt = JWTUtil.parseToken(token);
        Object id = jwt.getPayload("id");
        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",id);
        Teacher teacher = teacherMapper.selectOne(queryWrapper);
        if (teacher == null) {
            return "没有权限删除试卷";
        }else {
            QueryWrapper<ExamPaper> examPaperQueryWrapper = new QueryWrapper<>();
            examPaperQueryWrapper.eq("id",paperId);
            ExamPaper examPaper = examPaperMapper.selectOne(examPaperQueryWrapper);
            if (examPaper.getIsDel()==1) {
                return "该试卷已经删除";
            }else {
                examPaper.setIsDel(1);
                int update = examPaperMapper.updateById(examPaper);
                QueryWrapper<PaperDetail> paperDetailQueryWrapper = new QueryWrapper<>();
                paperDetailQueryWrapper.eq("paper_id",paperId);
                List<PaperDetail> paperDetails = paperDetailMapper.selectList(paperDetailQueryWrapper);
                for (PaperDetail paperDetail : paperDetails) {
                    paperDetail.setIsDel(1);
                    paperDetailMapper.updateById(paperDetail);
                }
                return "删除成功";
            }
        }
    }
}




